package com.mingqijia.gassafety.webserver.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gap.basic.exception.BizException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mingqijia.gassafety.db.entity.ConsumerDtuHistory;
import com.mingqijia.gassafety.db.entity.Supplier;
import com.mingqijia.gassafety.db.mapper.SupplierMapper;
import com.mingqijia.gassafety.shared.user.UserBackend;
import com.mingqijia.gassafety.shared.utils.ApplicationUtils;
import com.mingqijia.gassafety.shared.utils.commonUtils.PageUtil;
import com.mingqijia.gassafety.shared.vcc.VccAuthoritiesProvider;
import com.mingqijia.gassafety.webserver.request.QuerySupplierRequest;
import com.mingqijia.gassafety.webserver.request.SupplierRequest;
import com.mingqijia.gassafety.webserver.service.SupplierService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 供应商 服务实现类
 * </p>
 *
 * @author zhangfei
 * @since 2022-06-07
 */
@Service
@Slf4j
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements SupplierService {


    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private VccAuthoritiesProvider provider;

    /**
     * 新增
     *
     * @param request
     */
    @Override
    public void saveSupplier(SupplierRequest request) {
        //参数校验
        String spId = ApplicationUtils.getWorkingSpId();
        List<Supplier> suppliers = supplierMapper.selectBySupplierInfo(request.getSupplierName(),
                request.getForShort(), request.getSupplierCode(), spId, null);
        if (!CollectionUtil.isEmpty(suppliers)) {
            throw new BizException("供应商名称,简称,供应商代码存在重复");
        }
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(request, supplier);
        supplier.setSpId(spId);
        supplierMapper.insert(supplier);
    }

    /**
     * 编辑供应商
     *
     * @param request
     */
    @Override
    public void editSupplier(SupplierRequest request) {
        Long id = request.getId();
        if (StringUtils.isEmpty(id)) {
            throw new BizException("供应商id为空");
        }
        String spId = ApplicationUtils.getWorkingSpId();
        List<Supplier> suppliers = supplierMapper.selectBySupplierInfo(request.getSupplierName(),
                request.getForShort(), request.getSupplierCode(), spId, id);
        if (!CollectionUtil.isEmpty(suppliers)) {
            throw new BizException("编辑的供应商名称,简称,供应商代码存在重复");
        }
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(request, supplier);
        supplierMapper.updateById(supplier);
    }

    /**
     * 删除供应商
     *
     * @param id
     */
    @Override
    public void deleteSupplier(Long id) {
        Supplier supplier = new Supplier();
        supplier.setId(id);
        supplier.setIsDisabled(1);
        supplierMapper.updateById(supplier);
    }

    /**
     * 分页查询供应商列表
     *
     * @param request
     * @return
     */
    @Override
    public PageUtil<Supplier> querySupplier(QuerySupplierRequest request) {
        String spId = ApplicationUtils.getWorkingSpId();
        Page<ConsumerDtuHistory> page = PageHelper.startPage(request.getPage(), request.getPageSize());
        List<Supplier> suppliers = supplierMapper.selectListInfo(spId, request.getSupplierName());
        PageUtil pageInfo = new PageUtil<>(request.getPage(), request.getPageSize(), page.getTotal(), suppliers);
        return pageInfo;
    }

    @Override
    public Object queryEnterprise() {
        Object object = new Object();
        try {
            UserBackend workingUser = ApplicationUtils.getWorkingUser();
            String token = workingUser.getToken();
            log.info("queryEnterprise-token:{}", token);
            JSONObject orgAll = provider.getEnterpriseList(token);
            if (orgAll != null && orgAll.getJSONArray("items") != null) {
                object = orgAll.getJSONArray("items");
            }
        } catch (Exception e) {
            log.error("queryEnterprise-error:{}", e);
        }
        return object;
    }
}
